#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QPicture>
#include <QImage>
#include <QPainter>
#include <QFileDialog>
#include <QDir>
#include <QKeyEvent>
#include <QDebug>

QImage img(":img/imagens/megaman.gif");
QPainter painter;
QPoint point(0, 0);
QPixmap *pixmapSalvo;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    ativa(false)
{
    ui->setupUi(this);

    img = img.scaled(40, 34);

    QPixmap *pixmap = new QPixmap(":img/imagens/branco.png");
    pixmapSalvo = pixmap;

    painter.begin(pixmap);
    painter.drawImage(120, 205, img);

    painter.end();
    ui->labelFase->setPixmap(*pixmap);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::mousePressEvent(QMouseEvent *event){

    point = event->pos();
    this->ativa = true;
    this->repaint();
    qDebug() << "mouse Event\n";

}

void MainWindow::keyPressEvent(QKeyEvent *event){
    int key = event->key();

    switch(key){
        case Qt::Key_W:
            point.setX(120 + 40);
            point.setY(205 + 34);
            this->ativa = true;
            this->repaint();
            qDebug() << "apretou o w\n";
            break;
        default:
            break;

    }


}

void MainWindow::paintEvent(QPaintEvent *event){

    if(this->ativa){
        qDebug() << "paintEvent\n";
        QPixmap *pixmap = pixmapSalvo;
        painter.begin(pixmap);
        painter.drawImage(this->ui->labelFase->mapFromParent(point), img);
        painter.end();
        pixmapSalvo = pixmap;
        this->ui->labelFase->setPixmap(*pixmap);
        this->ativa = false;
    }
}
